package com.srwl.mytx.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.srwl.mytx.Constant;
import com.srwl.mytx.MyApplication;
import com.srwl.mytx.R;
import com.srwl.mytx.domain.AliOssService;
import com.srwl.mytx.domain.Topic;
import com.srwl.mytx.utils.GlideLoadUtils;
import com.srwl.mytx.utils.Utils;
import com.srwl.mytx.activity.TopicDetailsActivity;
import com.srwl.mytx.utils.DateUtils;


import java.util.LinkedHashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class HomePageTopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final String TAG = "HomePageTopicAdapter";
    private final List<Topic> topicList;
    private final Context context;
    private boolean bol_noData;//标记是否无数据
    private boolean bol_requestError;//标记是否有数据
    private long lastClickTime;
    private static int ITEM_VIDEO_TOPIC = 5;


    public HomePageTopicAdapter(Context context, List<Topic> topicList) {
        this.topicList = topicList;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            return new NoDataViewHolder(LayoutInflater.from(context).inflate(R.layout.no__data__layout, viewGroup, false));
        } else if (viewType == Constant.ITEM_TYPE_REQUEST_ERROR) {
            return new RequestErrorViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.request_error_layout, viewGroup, false));
        } else if (viewType == ITEM_VIDEO_TOPIC) {
            return new VideoTopicHolder(LayoutInflater.from(context).inflate(R.layout.topic_list_video_item, viewGroup, false));
        }
        return new PhotoTopicHolder(LayoutInflater.from(context).inflate(R.layout.topic_list_photo_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == Constant.ITEM_TYPE_NO_DATA) {
            NoDataViewHolder noDataViewHolder = (NoDataViewHolder) holder;
            noDataViewHolder.tv_tips.setText("没有数据");
        } else if (viewType == ITEM_VIDEO_TOPIC) {
            //视频主题
            Topic topic = topicList.get(position);
            VideoTopicHolder viewHolder = (VideoTopicHolder) holder;
            viewHolder.tv_title.setText(topic.getTitle());
            Integer type = topic.getType();
            if (type == Constant.TOPIC_TYPE_EXPOSE) {
                viewHolder.iv_title_head.setVisibility(View.VISIBLE);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_REWARD) {
                viewHolder.iv_title_head.setVisibility(View.GONE);
                viewHolder.rl_rewardIcon.setVisibility(View.VISIBLE);
                viewHolder.iv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setText(Utils.moneyFormat(topic.getPrice()));
            } else if (type == Constant.TOPIC_TYPE_OTHER) {
                viewHolder.iv_title_head.setVisibility(View.GONE);
            }

            viewHolder.tv_address.setText(topic.getAddress());
            viewHolder.videoPlayer.batteryLevel.setVisibility(View.GONE);
            viewHolder.videoPlayer.backButton.setVisibility(View.GONE);

            LinkedHashMap map = new LinkedHashMap();
            String url = topic.getVideo();
            String remoteUrl;
            if (url != null && url.startsWith("http")) {
                remoteUrl = url;
            } else {
                remoteUrl = AliOssService.TOPIC_VIDEO_PRE_URL + url;
            }
            JzvdStd.setVideoImageDisplayType(Jzvd.VIDEO_IMAGE_DISPLAY_TYPE_ORIGINAL);
            String proxyUrl = MyApplication.getProxy(context).getProxyUrl(remoteUrl);//视频缓存代理
            map.put("高清", proxyUrl);
            JZDataSource jzDataSource = new JZDataSource(map, "");
            jzDataSource.looping = true;
            // jzDataSource.currentUrlIndex = 2;
            viewHolder.videoPlayer.setUp(jzDataSource, JzvdStd.SCREEN_TINY);
            viewHolder.videoPlayer.fullscreenButton.setVisibility(View.INVISIBLE);
            // videoPlayer.bottomContainer.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.thumbImageView.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.startButton.setVisibility(View.VISIBLE);
            viewHolder.videoPlayer.thumbImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //Glide.with(context).load(remoteUrl).into(viewHolder.videoPlayer.thumbImageView);
            GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.videoPlayer.thumbImageView);


            if (topic.getUpdated() != null) {
                String updateDate = DateUtils.dateToString(topic.getUpdated(), "yyyy-MM-dd");
                viewHolder.tv_update_date.setText(updateDate);
            } else {
                String updateDate = DateUtils.dateToString(topic.getCreated(), "yyyy-MM-dd");
                viewHolder.tv_update_date.setText(updateDate);
            }


            //加载收藏 评论 点赞 踩的数量
            // TODO: 2019/4/17  收藏数量
            //viewHolder.tv_collection_num.setText("还未创建改字段");
            viewHolder.tv_comment_num.setText(Utils.numberFormat2(topic.getCommentSize()));
            viewHolder.tv_like_num.setText(Utils.numberFormat2(topic.getPraiseSize()));

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime < 1000) {
                        return;
                    }
                    lastClickTime = currentTime;
                    context.startActivity(new Intent(context, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, topic));
                }
            });
        } else {//图片主题
            Topic topic = topicList.get(position);
            PhotoTopicHolder viewHolder = (PhotoTopicHolder) holder;
            viewHolder.tv_title.setText(topic.getTitle());
            Integer type = topic.getType();
            if (type == Constant.TOPIC_TYPE_EXPOSE) {
                viewHolder.iv_title_head.setVisibility(View.VISIBLE);
                viewHolder.rl_rewardIcon.setVisibility(View.GONE);
                viewHolder.iv_reward.setVisibility(View.INVISIBLE);
                viewHolder.tv_reward.setVisibility(View.INVISIBLE);
            } else if (type == Constant.TOPIC_TYPE_REWARD) {
                viewHolder.iv_title_head.setVisibility(View.GONE);
                viewHolder.rl_rewardIcon.setVisibility(View.VISIBLE);
                viewHolder.iv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setVisibility(View.VISIBLE);
                viewHolder.tv_reward.setText(Utils.moneyFormat(topic.getPrice()));
                //viewHolder.tv_reward.setText(topic.getReward() + "");
            } else if (type == Constant.TOPIC_TYPE_OTHER) {
                viewHolder.iv_title_head.setVisibility(View.GONE);
            }

            viewHolder.tv_address.setText(topic.getAddress());
            {
                String url = topic.getPictures().get(0);
                String remoteUrl = generateFullUrl(url);
                // Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_01);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_01);

            }
            if (topic.getPictures().size() > 1) {
                String url = topic.getPictures().get(1);

                String remoteUrl = generateFullUrl(url);
                // Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_02);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_02);

            }

            if (topic.getPictures().size() > 2) {
                String url = topic.getPictures().get(2);
                String remoteUrl = generateFullUrl(url);
                // Glide.with(context).load(remoteUrl).into(viewHolder.iv_preview_03);
                GlideLoadUtils.glideLoad(context, remoteUrl, viewHolder.iv_preview_03);

            }
            viewHolder.tv_comment_num.setText(Utils.numberFormat2(topic.getCommentSize()));
            viewHolder.tv_like_num.setText(Utils.numberFormat2(topic.getPraiseSize()));
            if (topic.getUpdated() != null) {
                String updateDate = DateUtils.dateToString(topic.getUpdated(), "yyyy-MM-dd");
                viewHolder.tv_update_date.setText(updateDate);
            } else {
                String updateDate = DateUtils.dateToString(topic.getCreated(), "yyyy-MM-dd");
                viewHolder.tv_update_date.setText(updateDate);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    long currentTime = System.currentTimeMillis();
                    if (currentTime - lastClickTime < 1000) {
                        return;
                    }
                    lastClickTime = currentTime;
                    context.startActivity(new Intent(context, TopicDetailsActivity.class).putExtra(Constant.EXTRA_INFO_TOPIC, topic));
                }
            });
        }

    }

    private String generateFullUrl(String url) {
        String remoteUrl;
        if (url != null && url.startsWith("http")) {
            remoteUrl = url;
        } else {
            remoteUrl = AliOssService.TOPIC_IMAGE_PRE_URL + url;
        }
        return remoteUrl;
    }


    @Override
    public int getItemCount() {
        if (bol_noData || bol_requestError) {
            return 1;
        }
        return topicList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (bol_noData) {
            return Constant.ITEM_TYPE_NO_DATA;
        } else if (bol_requestError) {
            return Constant.ITEM_TYPE_REQUEST_ERROR;
        } else if (!TextUtils.isEmpty(topicList.get(position).getVideo())) {
            return ITEM_VIDEO_TOPIC;
        }
        return Constant.ITEM_TYPE_NORMAL;
    }

    class VideoTopicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.iv_reward)
        ImageView iv_reward;
        @BindView(R.id.tv_money_amount)
        TextView tv_reward;
        @BindView(R.id.iv_title_head)
        ImageView iv_title_head;
        @BindView(R.id.rl_reward_icon)
        LinearLayout rl_rewardIcon;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.video_player)
        JzvdStd videoPlayer;

        @BindView(R.id.tv_collection_num)
        TextView tv_collection_num;
        @BindView(R.id.tv_comment_num)
        TextView tv_comment_num;
        @BindView(R.id.tv_like_num)
        TextView tv_like_num;
        @BindView(R.id.tv_dislike_num)
        TextView tv_dislike_num;
        @BindView(R.id.tv_update_date)
        TextView tv_update_date;
        @BindView(R.id.iv_collection)
        ImageView iv_collection;
        @BindView(R.id.iv_like)
        ImageView iv_like;
        @BindView(R.id.iv_dislike)
        ImageView iv_dislike;


        public VideoTopicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }

    class PhotoTopicHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_title)
        TextView tv_title;
        @BindView(R.id.iv_reward)
        ImageView iv_reward;
        @BindView(R.id.tv_money_amount)
        TextView tv_reward;
        @BindView(R.id.iv_title_head)
        ImageView iv_title_head;
        @BindView(R.id.rl_reward_icon)
        LinearLayout rl_rewardIcon;
        @BindView(R.id.tv_address)
        TextView tv_address;
        @BindView(R.id.iv_preview_01)
        ImageView iv_preview_01;
        @BindView(R.id.iv_preview_02)
        ImageView iv_preview_02;
        @BindView(R.id.iv_preview_03)
        ImageView iv_preview_03;
        @BindView(R.id.tv_collection_num)
        TextView tv_collection_num;
        @BindView(R.id.tv_comment_num)
        TextView tv_comment_num;
        @BindView(R.id.tv_like_num)
        TextView tv_like_num;
        @BindView(R.id.tv_dislike_num)
        TextView tv_dislike_num;
        @BindView(R.id.tv_update_date)
        TextView tv_update_date;
        @BindView(R.id.iv_collection)
        ImageView iv_collection;
        @BindView(R.id.iv_like)
        ImageView iv_like;
        @BindView(R.id.iv_dislike)
        ImageView iv_dislike;


        public PhotoTopicHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    /**
     * 显示普通视图
     * 有数据的时候调用
     */
    public void showNormalView() {
        bol_noData = false;
        bol_requestError = false;
        notifyDataSetChanged();
    }

    /**
     * 显示无数据视图
     */
    public void showNoDataView() {
        bol_noData = true;
        notifyDataSetChanged();
    }

    /**
     * 显示请求数据错误视图
     */
    public void showErrorView() {
        bol_requestError = true;
        notifyDataSetChanged();
    }
}
